package cn.kmsoft.common;

import io.swagger.annotations.ApiModel;

import java.io.Serializable;

/**
 * 返回内容统一封装v2.0版本
 *
 */
public class ResInfo implements Serializable {
    public static final String SUCCESS = "success";//业务操作成功
    public static final String FAIL = "failed";// 业务操作失败

    public static final String OUT = "O";// session过期
    public static final String ERROR = "E";// 系统错误

    private Integer code;//返回码，200-成功
    private String msg;//消息说明
    private String status = SUCCESS;//状态标识：success与failed
    private Object data;//返回的结果数据
    private Object res;

    public ResInfo(){}
    public ResInfo(String status,Integer code,String msg,Object data){
        this.status= status;
        this.code=code;
        this.msg=msg;
        this.data= data;
    }

    /**
     * success：200；failed：其他
     * @return
     */
    public Integer getCode() {
        return code;
    }
    public ResInfo setCode(Integer code) {
        this.code = code;
        return this;
    }

    /**
     * 执行状态：success、failed
     * @return
     */
    public String getStatus() {
        return status;
    }
    public ResInfo setStatus(String status) {
        this.status = status;
        return this;
    }

    public Object getRes() {
        return res;
    }
    public ResInfo setRes(Object res) {
        this.res = res;
        return this;
    }

    /**
     * 返回的数据
     * @return
     */
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 简要描述性的消息
     */
    public String getMsg() {
        return msg;
    }
    public ResInfo setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    //成功
    public static ResInfo success(String message) {
        return new ResInfo(ResultEnum.SUCCESS.getStatus(), ResultEnum.SUCCESS.getCode(), message, null);
    }
    public static ResInfo success(Object data) {
        return new ResInfo(ResultEnum.SUCCESS.getStatus(), ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);
    }
    public static ResInfo success(String message, Object data) {
        return new ResInfo(ResultEnum.SUCCESS.getStatus(), ResultEnum.SUCCESS.getCode(), message, data);
    }

    //失败
    public static ResInfo failed() {
        return new ResInfo(ResultEnum.COMMON_FAILED.getStatus(), ResultEnum.COMMON_FAILED.getCode(), ResultEnum.COMMON_FAILED.getMessage(), null);
    }
    public static ResInfo failed(String message) {
        return new ResInfo(ResultEnum.COMMON_FAILED.getStatus(), ResultEnum.COMMON_FAILED.getCode(), message, null);
    }
    public static ResInfo failed(IResult errorResult) {
        return new ResInfo(errorResult.getStatus(), errorResult.getCode(), errorResult.getMessage(), null);
    }
    public static ResInfo failed(IResult errorResult, String detailMsg) {
        String msg = String.format("%s, detailMsg: %s",errorResult.getMessage(),detailMsg);
        return new ResInfo(errorResult.getStatus(), errorResult.getCode(), msg, null);
    }

}
